package com.arelikebrothers.demo.concurrent.algo.pc;

import java.util.Random;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;

/**
 * Created by lennon on 04/05/2017.
 */
public class Consumer implements Runnable {

    private volatile boolean isRuning = true;

    private BlockingQueue<PCData> queue;

    public Consumer(BlockingQueue<PCData> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        Random r = new Random();
        try {
            while (isRuning) {
                PCData data = queue.take();
                if (data != null) {
                    long re = data.getData() * data.getData();
                    System.out.println(String.format("%s * %s = %s",
                            data.getData(), data.getData(), re));
//                    Thread.sleep(r.nextInt(1000));

                }

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.interrupted();
        }

    }

    public void stop() {
        isRuning = false;
    }
}
